package com.mars.manager.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.mars.component.scheduler.entity.SchedulerClassDict;
import com.mars.component.scheduler.entity.SchedulerJob;
import com.mars.component.scheduler.entity.SchedulerJobLog;
import com.mars.component.scheduler.service.SchedulerClassDictService;
import com.mars.component.scheduler.service.SchedulerJobLogService;
import com.mars.component.scheduler.service.SchedulerJobService;
import com.mars.manager.user.exception.UserBizException;
import com.mars.manager.web.vo.SchedulerJobVo;
import com.mars.web.common.pojo.DataGrid;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping(value="/scheduler")
public class SchedulerController extends BaseController {
	
	/**
	 * 可执行的任务类字典
	 */
	@Autowired
	private SchedulerClassDictService schedulerClassDictService ;
	
	/**
	 * 定时任务调度
	 */
	@Autowired
	private SchedulerJobService schedulerJobService ;
	
	/**
	 * 定时任务执行日志
	 */
	@Autowired
	private SchedulerJobLogService schedulerJobLogService ;

	
	/** ============================================================================================================= */
	@RequestMapping(value="/classDict/main", method=RequestMethod.GET)
	public String classDictMain() {
		return viewName("classDict/main") ;
	}
	
	@RequestMapping(value="/classDict/form", method=RequestMethod.GET)
	public String classDictForm(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("classDict/form") ;
	}
	
	@PostMapping(value="/classDict/add")
	public Object classDictAdd(SchedulerClassDict entity) {
		try {
			this.schedulerClassDictService.insertSelective(entity) ;
 		} catch (Exception e) {
 			return setResponseResultFailed(e.getMessage()) ;
 		}
		return setResponseResultAddOk() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/classDict/delete/{ids}", method=RequestMethod.GET)
	public Object classDictDelete(@PathVariable String ids) {
		try {
			this.schedulerClassDictService.deleteByIds(ids) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@ResponseBody
	@RequestMapping(value="/classDict/update", method=RequestMethod.POST)
	public Object classDictUpdate(SchedulerClassDict entity) {
		try {
			this.schedulerClassDictService.updateSelective(entity) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@ResponseBody
	@RequestMapping(value="/classDict/get/{id}", method=RequestMethod.GET)
	public Object classDictGet(@PathVariable Long id) {
		try {
			return this.schedulerClassDictService.selectById(id) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/classDict/list", method={RequestMethod.GET, RequestMethod.POST})
	public Object classDictList(SchedulerClassDict entity, Integer page, Integer rows) {
		try {
			PageInfo<SchedulerClassDict> selectPage = this.schedulerClassDictService.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<SchedulerClassDict> dg = new DataGrid<SchedulerClassDict>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/classDict/combobox", method=RequestMethod.GET)
	public Object classDictCombobox(SchedulerClassDict entity) {
		try {
			return this.schedulerClassDictService.selectList(entity) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	
	/** ============================================================================================================= */
	
	@RequestMapping(value="/job/main", method=RequestMethod.GET)
	public String jobMain() {
		return viewName("job/main") ;
	}
	
	@RequestMapping(value="/job/form", method=RequestMethod.GET)
	public String jobForm(Long jobId, Model model) {
		if(null != jobId) {
			model.addAttribute("jobId", jobId) ;
		}
		return viewName("job/form") ;
	}
	
	@PostMapping(value="/job/createScheduleJob")
	public Object createScheduleJob(SchedulerJob schedulerJob) {
		try {
			this.schedulerJobService.createScheduleJob(schedulerJob);
		} catch (Exception e) {
			return setResponseResultFailed(e.getMessage()) ;
		}
		return setResponseResultAddOk() ;
	}
	
	@PostMapping(value="/job/rescheduleJob")
	public Object rescheduleJob(SchedulerJob schedulerJob) {
		try {
			this.schedulerJobService.rescheduleJob(schedulerJob);
		} catch (Exception e) {
			return setResponseResultFailed(e.getMessage()) ;
		}
		return setResponseResultAddOk() ;
	}
	
	@PostMapping(value="/job/pauseJob")
	public Object pauseJob(@RequestBody SchedulerJobVo vo) {
		try {
			this.schedulerJobService.pauseJob(vo.getJobIds(), vo.getJobGroups());
		} catch (Exception e) {
			return setResponseResultFailed(e.getMessage()) ;
		}
		return setResponseResultAddOk() ;
	}
	
	@PostMapping(value="/job/resumeJob")
	public Object resumeJob(@RequestBody SchedulerJobVo vo) {
		try {
			this.schedulerJobService.resumeJob(vo.getJobIds(), vo.getJobGroups());
		} catch (Exception e) {
			return setResponseResultFailed(e.getMessage()) ;
		}
		return setResponseResultAddOk() ;
	}
	
	@PostMapping(value="/job/deleteJob")
	public Object deleteJob(@RequestBody SchedulerJobVo vo) {
		try {
			this.schedulerJobService.deleteJob(vo.getJobIds(), vo.getJobGroups());
		} catch (Exception e) {
			return setResponseResultFailed(e.getMessage()) ;
		}
		return setResponseResultAddOk() ;
	}
	
	@PostMapping(value="/job/runNowJob")
	public Object runNowJob(@RequestBody SchedulerJobVo vo) {
		try {
			this.schedulerJobService.runNowJob(vo.getJobIds());
 		} catch (Exception e) {
 			return setResponseResultFailed(e.getMessage()) ;
 		}
		return setResponseResultAddOk() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/job/get/{id}", method=RequestMethod.GET)
	public Object jobGet(@PathVariable Long id) {
		try {
			return this.schedulerJobService.selectById(id) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/job/list", method={RequestMethod.GET, RequestMethod.POST})
	public Object jobList(SchedulerJob entity, Integer page, Integer rows) {
		try {
			PageInfo<SchedulerJob> selectPage = this.schedulerJobService.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<SchedulerJob> dg = new DataGrid<SchedulerJob>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	
	/** ============================================================================================================= */
	
	@RequestMapping(value="/jobLog/main", method=RequestMethod.GET)
	public String jobLogMain() {
		return viewName("jobLog/main") ;
	}
	
	@RequestMapping(value="/jobLog/form", method=RequestMethod.GET)
	public String jobLogForm(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("jobLog/form") ;
	}
	

	@PostMapping(value="/jobLog/add")
	public Object jobLogAdd(SchedulerJobLog entity) {
		try {
			this.schedulerJobLogService.insertSelective(entity) ;
 		} catch (Exception e) {
 			return setResponseResultFailed(e.getMessage()) ;
 		}
		return setResponseResultAddOk() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/jobLog/delete/{ids}", method=RequestMethod.GET)
	public Object jobLogDelete(@PathVariable String ids) {
		try {
			this.schedulerJobLogService.deleteByIds(ids) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@ResponseBody
	@RequestMapping(value="/jobLog/update", method=RequestMethod.POST)
	public Object jobLogUpdate(SchedulerJobLog entity) {
		try {
			this.schedulerJobLogService.updateSelective(entity) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@ResponseBody
	@RequestMapping(value="/jobLog/get/{id}", method=RequestMethod.GET)
	public Object jobLogGet(@PathVariable Long id) {
		try {
			return this.schedulerJobLogService.selectById(id) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/jobLog/list", method={RequestMethod.GET, RequestMethod.POST})
	public Object jobLogList(SchedulerJobLog entity, Integer page, Integer rows) {
		try {
			PageInfo<SchedulerJobLog> selectPage = this.schedulerJobLogService.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<SchedulerJobLog> dg = new DataGrid<SchedulerJobLog>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
}
